package com.example.mghotel.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.example.mghotel.R;
import com.example.mghotel.ui.home.HomeFragment;
import com.example.mghotel.ui.order.OrderFragment;
import com.example.mghotel.ui.user.UserFragment;

public class MainActivity extends AppCompatActivity {

    private final String TAG="MainActivity";


    Fragment[] fragments=new Fragment[]{null,null,null};//用一个列表存放三个栏目（碎片）
    private LinearLayout llContent;//内容容器
    private RadioButton rbHome;//rb 主页
    private RadioButton rbOrder;//rb订单
    private RadioButton viewById;//rb 我的
    private RadioButton rbMy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取控件
        llContent = findViewById(R.id.ll_main_content);
        rbHome = findViewById(R.id.rb_main_home);
        viewById = findViewById(R.id.rb_main_order);
        rbOrder = viewById;
        rbMy = findViewById(R.id.rb_main_my);


        //页面初始化
        initView();

        //设置控件点击事件
        setViewEventListener();
    }

    private void setViewEventListener() {
        //HomeFragment
        rbHome.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                switchFragment(0);
            }
        });
        //OrderFragment
       rbOrder.setOnClickListener(new View.OnClickListener(){
           @Override
           public void onClick(View v) {
               switchFragment(1);
           }
       });
        //UserFragment
        rbMy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchFragment(2);
            }
        });


    }


    private void initView() {

        //默认选中主页
        rbHome.setChecked(true);
        switchFragment(0);

        //默认选中全部订单
        //rbOrderAll.setChecked(true);
    }


    private void switchFragment(int fragmentIndex){

        //在Activity中显示Fragment
        FragmentManager fragmentManager=this.getSupportFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();

        //懒加载
        if (fragments[fragmentIndex]==null){
            switch (fragmentIndex){
                case 0://主页
                    fragments[fragmentIndex]=new HomeFragment();
                    break;
                case 1://订单
                    fragments[fragmentIndex]=new OrderFragment();
                    break;
                case 2://我的
                    fragments[fragmentIndex]=new UserFragment();
                    break;
            }

            transaction.add(R.id.ll_main_content,fragments[fragmentIndex]);
        }

        //隐藏其他fragment
        for (int i=0;i<fragments.length;i++){
            if (fragmentIndex!=i && fragments[i]!=null){
                transaction.hide(fragments[i]);
            }
        }

        transaction.show(fragments[fragmentIndex]);
        transaction.commit();
    }
}